﻿using FastCloud.Base.Attribute;
using FastCloud.Base.Config;
using FastUtil.Config;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FastCloud.Base.Filter
{
    [CloudFilter(SoryNum = 89)]
    public class TokenAuthFilter : ActionFilterAttribute
    {
        private TokenUtil TokenUtil = (TokenUtil)InjectContext.Resolve(typeof(TokenUtil));
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            var Request = context.HttpContext.Request;
            var Response = context.HttpContext.Response;
            var ControllerEntity = context.ActionDescriptor as ControllerActionDescriptor;
            //配置文件配置的匿名访问
            bool IsConfigAnonymous = CloudUtil.CheckIsAnonymousAccess(Request.Path);
            //代码标注的匿名访问
            bool IsCodeControolerAnonymous = context.Controller.GetType().GetCustomAttributes(typeof(AllowAnonymous), true).Count() > 0;
            bool IsCodeActionAnonymous = ControllerEntity.MethodInfo.GetCustomAttributes(typeof(AllowAnonymous), true).Count() > 0;
            bool IsAuthController = context.Controller.GetType().GetCustomAttributes(typeof(TokenAuth), true).Count() > 0;
            if (IsAuthController && !IsConfigAnonymous)
            {
                //如果单个方法标记了允许匿名访问，这边不验证Token
                if (IsCodeControolerAnonymous || IsCodeActionAnonymous)
                {
                    base.OnActionExecuting(context);
                }
                else
                {
                    if (TokenUtil.ValidToken())
                    {
                        //刷新Token的时间
                        TokenUtil.RefreshToken();
                        base.OnActionExecuting(context);
                    }
                    else
                    {
                        bool IsAjaxCall = context.HttpContext.Request.Headers["x-requested-with"] == "XMLHttpRequest";
                        if (IsAjaxCall)
                        {
                            context.Result = new RedirectResult(CloudUtil.GetRootPath() + "frame/home/tokenerror");
                        }
                        else
                        {
                            context.Result = new RedirectResult(CloudUtil.GetRootPath() + "frame/home/login");
                        }
                    }
                }
            }
            else
            {
                if (TokenUtil.ValidToken() && Request.Path.ToString().Contains("frame/home/login"))
                {
                    //如果已登陆，访问登录页自动跳转主页
                    context.Result = new RedirectResult(CloudUtil.GetRootPath() + AppConfigUtil.Configuration["Frame:DefaultHomeUrl"]);
                }
                else
                {
                    base.OnActionExecuting(context);
                }

            }
            base.OnActionExecuting(context);

        }
    }
}
